package cn.gmcx.lazy.izze.controller;


import cn.gmcx.lazy.izze.enums.DynamicTypeEnum;
import cn.gmcx.lazy.izze.exception.BizException;
import cn.gmcx.lazy.izze.pojo.Dynamic;
import cn.gmcx.lazy.izze.pojo.User;
import cn.gmcx.lazy.izze.service.FileService;
import cn.gmcx.lazy.izze.service.IDynamicService;
import cn.gmcx.lazy.izze.service.IUserService;
import cn.gmcx.lazy.izze.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.micrometer.common.util.StringUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author GMCX
 * @since 2024-06-14
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private FileService fileService;

    @Autowired
    private IDynamicService dynamicService;
    /**
     *
     * @param user
     * @param
     * <p>
     *     修改用户信息
     * </p>
     * @return
     */
    @PostMapping("/update_user")
    public Result updateUser( @RequestBody User user) {
        if (user.getUsername().isBlank()){
            throw new BizException("用户名不能为空！");
        }
        boolean b = userService.updateById(user);
        if(b){
            dynamicService.addDynamic(user.getId(),DynamicTypeEnum.UPDATE,"个人信息");
            return new Result(200,"修改成功");
        }else{
            return new Result(500,"系统出错");
        }
    }

    /**
     * @param id
     * @param file <p>
     *             更新头像
     *             </p>
     * @return
     */
    @PostMapping("/update_user_avatar")
    public Result updateAvatar(Integer id, MultipartFile file) {
        if (file.isEmpty()) {
            return new Result(500, "更新失败,文件为空", null);
        }
        User user = userService.selectById(id);
        String fileUrl = user.getAvatar();
        boolean s = fileService.remove_file2(fileUrl);
        if (s) {
            String imgUrl = fileService.getImg_url2(file);
            User upUser = new User();
            upUser.setId(user.getId());
            upUser.setAvatar(imgUrl);
            boolean b = userService.updateById(upUser);
            if (b) {
                dynamicService.addDynamic(id,DynamicTypeEnum.UPDATE,"头像");
                return new Result(200, "更新成功", null);
            }
        }
        return new Result(500, "更新失败", null);
    }

    @PostMapping("/delete_user")
    public Result deleteUser(Integer id) {
        userService.deleteById(id);
        return Result.success("删除成功");
    }

    @GetMapping("/selectAll_user")
    public Result selectAll() {
        List<User> users = userService.selectAll();
        return Result.success(users);
    }

    @GetMapping("/selectById_user")
    public Result selectById(Integer id) {
        User user = userService.selectById(id);
        user.setPassword(null);
        return Result.success(user);
    }

    @GetMapping("/user_select_izze")
    public Result selectUserByIzzeId(@RequestParam("value") String value){
        if (value.isBlank()){
            return new Result(500,"未输入查询内容");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("izze_code",value);
        List<User> list = userService.list(wrapper);
        list.forEach(item ->{
            item.setPassword(null);
        });
        return Result.success(list);
    }
    @GetMapping("/user_select_username")
    public Result selectUserByUsername(@RequestParam("value") String value){
        if (value.isBlank()){
            return new Result(500,"未输入查询内容");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username",value);
        List<User> list = userService.list(wrapper);
        list.forEach(item ->{
            item.setPassword(null);
        });
        return Result.success(list);
    }
}
